package com.rocky.project.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
import com.rocky.project.model.admin.User;
import com.rocky.project.model.base.BaseEntity;

/**
 * @author rocky.shen
 *
 */
public final class WebUtil {
	
	public static final String sessionkey_comefromURL ="session.key.comefromURL";	
	public static final String sessionkey_user ="session.key.user";	
	public static final String loginRRL ="/admin/loginLink.action";	
	public static final String doLoginRRL ="/admin/doLogin.action";
	public static final String isSystemAbled ="com.rocky.project.abled";
	protected static Logger log = Logger.getLogger(WebUtil.class);
	private static Properties config;
	private static void readConfig() {
		if(config==null){
			config = new Properties();
		}
		try {
			String s_config="config.properties";
			String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/"+s_config);
//			InputStream inputFile = ClassLoader.getSystemResourceAsStream(path);
			FileInputStream inputFile = new FileInputStream(path);
			config.load(inputFile);
			inputFile.close();
		} catch (FileNotFoundException ex) {
			log.error(ex.getMessage());
			log.error("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
		} catch (IOException ex) {
			log.error(ex.getMessage());
			log.error("装载文件--->失败!");			
		}
	}
	/**
	 * getParaValue
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static String getConfigValue(String key) {
		readConfig();
		if (config.containsKey(key)) {
			return config.getProperty(key);
		} 
		return null;
	}

	/**
	 * getParaValue
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static void setConfigValue(String key,String value) {
		readConfig();
		config.setProperty(key, value);
//		saveConfig();
	}	
	/**
	 * getParaValue
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static void saveConfig() {
		try {
			FileOutputStream outputFile = new FileOutputStream(ClassLoader.getSystemResource("config.properties").getFile());
			config.store(outputFile,"rocky.shen modified");
			outputFile.close();
		} catch (FileNotFoundException e) {
			log.error("saveConfig:FileNotFoundException--->失败!");				
		} catch (IOException ioe) {
			log.error("saveConfig:IOException--->失败!");	
		}
	}
	
	
	/**
	 * getParameters
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static Map getParameters() {
		return ActionContext.getContext().getParameters();
	}

	/**
	 * getParaValue
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static String getParaValue(String key) {
		String[] values = getParaValues(key);
		if (values != null && values.length > 0) {
			return values[0];
		}
		return null;
	}

	/**
	 * getParaValues
	 * 
	 * @author rocky.shen 20070625
	 * @return
	 */
	public static String[] getParaValues(String key) {

		Object value = getParameters().get(key);
		if (value instanceof String) {
			return new String[] { value.toString() };
		}
		if (value instanceof String[]) {
			return (String[]) value;
		}
		if (value instanceof Object[]) {
			Object[] value2 = (Object[]) value;
			List<String> result = new ArrayList<String>();
			for (Object each : value2) {
				result.add(each.toString());
			}
			return (String[]) result.toArray();
		}
		return null;

	}
	/**
	 * initResult
	 * 
	 * @author rocky.shen 20070625
	 * @param result
	 * @param pagesize
	 * @return
	 */
	public static Result initResult(Result result, int pagesize) {
		Result newResult = result;
		if (newResult == null) {
			newResult = new Result();
			newResult.setPage(PageUtil.createPage(pagesize, 0, 0));// 设置三个默认值，实际使用的时候会覆盖
		}
		return newResult;
	}

	/**
	 * updateResult
	 * 
	 * @author rocky.shen 20070625
	 * @param result
	 * @param totalRecords
	 * @return
	 */
	public static Result updateResult(Result result, int totalRecords) {
		Result newResult = result;
		Page page = newResult.getPage();
		page = PageUtil.createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
		newResult.setPage(page);
		return newResult;
	}
	
	public static void init4create(BaseEntity baseEntity){
		String createIp = ServletActionContext.getRequest().getLocalAddr();
		User user = getSessionUser();
		baseEntity.setCreateIp(createIp);
		if(user!=null){
			baseEntity.setCreator(user.getUsername());
		}else{
			baseEntity.setCreator("regOrNotlogin");	
		}
		
	}
	
	public static void init4modify(BaseEntity baseEntity){
		String createIp = ServletActionContext.getRequest().getLocalAddr();
		User user = getSessionUser();
		baseEntity.setModifyIp(createIp);
		baseEntity.setModifyTime(new Date());
		if(user!=null){
			baseEntity.setModifier(user.getUsername());
		}else{
			baseEntity.setModifier("notlogin");	
		}
		
	}

	public static User getSessionUser() {
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute(sessionkey_user);
		return user;
	}
	
	public static void setSessionUser(User user){
		ServletActionContext.getRequest().getSession().setAttribute(sessionkey_user,user);

	}
	public static void removeSessionUser(){
		ServletActionContext.getRequest().getSession().setAttribute(sessionkey_user,null);
		ServletActionContext.getRequest().getSession().removeAttribute(sessionkey_user);
	}
	
	





	
	

}
